package com.atguigu.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.atguigu.dao.UserDao;
import com.atguigu.dao.impl.UserDaoImpl;

/**
 * 通过发送Ajax请求验证用户名是否可用的Servlet
 */
public class CheckUsernameServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取用户名
		String username = request.getParameter("username");
		//创建UserDao对象
		UserDao userDao = new UserDaoImpl();
		//调用UserDao中验证用户名是否存在的方法
		boolean flag = userDao.checkUsername(username);
		response.setContentType("text/html;charset=UTF-8");
		if(flag) {
			//用户名已存在
			response.getWriter().write("用户名已存在！");
		}else {
			//用户名可用
			response.getWriter().write("<font style='color:green'>用户名可用！</font>");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
